package com.qf.controller;

import com.alibaba.druid.util.StringUtils;
import com.qf.pojo.*;
import com.qf.service.DtsBrandService;
import com.qf.service.DtsCategoryService;
import com.qf.service.DtsGoodsService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import com.qf.vo.GoodsAllinone;
import io.swagger.models.auth.In;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/10/17 19:26
 */

@RestController
@RequestMapping("/admin/goods")
public class AdminGoodsController {

    @Autowired
    DtsGoodsService goodsService;
    @Autowired
    DtsBrandService brandService;
    @Autowired
    DtsCategoryService categoryService;
    /**
     * 1. 判断是否是品牌制造商
     * 2. 如果是品牌制造商，只能查看对应品牌的商品数据
     * 3. 如果不是品牌制造商，查看所有商品
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param goodsSn
     * @param name
     * @return
     */
    @GetMapping("/list")
    public Object list(@RequestParam(value = "page",required = false, defaultValue = "1") Integer page,
                       @RequestParam(value = "limit",required = false, defaultValue = "20")Integer limit,
                       @RequestParam(value = "sort",required = false, defaultValue = "add_time")String sort,
                       @RequestParam(value = "order",required = false, defaultValue = "desc")String order,
                       String goodsSn, String name){
//      获取当前登录的用户
        Subject subject = SecurityUtils.getSubject();
        DtsAdmin admin = (DtsAdmin) subject.getPrincipal();
        List<Integer> brandIds = null;
        if (subject.hasRole("品牌制造商")){
            //如果是品牌制造商，只能查看对应品牌的商品数据
            //查询 该管理员对应的品牌id
            brandIds = brandService.findBrandByAdminId(admin.getId());
        }

        Map<String, Object> data = goodsService.findByPage(page,limit,sort,order,goodsSn,name,brandIds);

        return ResponseUtil.ok(data);
    }

    @GetMapping("/catAndBrand")
    public Object catAndBrand(){
//        查询分类列表
        List<CatVo> categoryList = categoryService.findCategoryList();
//        查询品牌列表
        List<Map<String,Object>> brandList = brandService.findBrandList();

        Map<String, Object> map = new HashMap<>();
        map.put("categoryList",categoryList);
        map.put("brandList",brandList);

        return ResponseUtil.ok(map);
    }


    /**
     1. 校验参数
     2. 根据商品名字判断此商品是否已售卖
     3. 添加商品SPU, 商品基本信息表Dts_goods
     4. 添加商品规格数据, 商品规格表Dts_goods_specification
     5. 添加商品参数数据, 商品参数表Dts_goods_attribute
     6. 添加商品库存数据SKU, 商品库存表Dts_product
     * @param allinone
     * @return
     */
    @PostMapping("/create")
    public Object create(@RequestBody GoodsAllinone allinone){

        Object result = checkGoodsAllinone(allinone);
        if (result != null){
            return result;
        }

        try {
            goodsService.insert(allinone);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(-1,e.getMessage());
        }

        return ResponseUtil.ok();
    }

//    商品修改回显
    @GetMapping("/detail")
    public Object detail(Integer id){

        Map<String,Object> data = goodsService.detail(id);

        return ResponseUtil.ok(data);
    }

    @PostMapping("/update")
    public Object update(@RequestBody GoodsAllinone goodsAllinone){
        Object result = checkGoodsAllinone(goodsAllinone);
        if (result != null){
            return result;
        }

        try {
            goodsService.update(goodsAllinone);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(-1,e.getMessage());
        }
        return ResponseUtil.ok();
    }

    @PostMapping("/delete")
    public Object delete(@RequestBody DtsGoods goods){

        goodsService.delete(goods);

        return ResponseUtil.ok();
    }

    /**
     * 1. 判断商品名称是否为空
     * 2. 判断商品编号是否为空
     * 3. 牌商可以不设置，如果设置则需要验证品牌商存在
     * 4. 分类可以不设置，如果设置则需要验证分类存在
     * 5. 判断商品属性数据是否为空
     * 6. 判断商品规格数据是否为空
     * 7. 判断商品库存数据是否为空SKU
     * @param allinone
     * @return
     */
    private Object checkGoodsAllinone(GoodsAllinone allinone) {
//        private DtsGoods goods;
//        private DtsGoodsSpecification[] specifications;
//        private DtsGoodsAttribute  [] attributes;
        // 这里采用 Product 再转换到 DtsGoodsProduct
//        private DtsGoodsProduct[] products;
        DtsGoods goods = allinone.getGoods();
        if (goods==null || StringUtils.isEmpty(goods.getName()) || StringUtils.isEmpty(goods.getGoodsSn())){
            return ResponseUtil.fail(-1,"商品名称和编号不能为空");
        }

        DtsGoodsSpecification[] specifications = allinone.getSpecifications();
        if (specifications != null || specifications.length > 0){
            for (DtsGoodsSpecification specification : specifications) {
                if (StringUtils.isEmpty(specification.getSpecification())){
                    return ResponseUtil.fail(-1,"商品规格不能为空");
                }
            }
        }

        DtsGoodsAttribute[] attributes = allinone.getAttributes();
        if (attributes != null ||attributes.length > 0){
            for (DtsGoodsAttribute attribute : attributes) {
                if (StringUtils.isEmpty(attribute.getAttribute())){
                    return ResponseUtil.fail(-1,"商品属性名称不能为空");
                }
            }
        }

        DtsGoodsProduct[] products = allinone.getProducts();
        if (products == null || products.length == 0){
            return ResponseUtil.fail(-1,"商品库存不能为空");
        }

//        验证品牌商存在
        Integer brandId = goods.getBrandId();
        if (brandId != null){
            DtsBrand brand = brandService.findById(brandId);
            if (brand == null){
                return ResponseUtil.fail(-1,"商品品牌不存在");
            }
        }
//        验证分类存在
        Integer categoryId = goods.getCategoryId();
        if (categoryId!=null){
            DtsCategory category = categoryService.findById(categoryId);
            if (category == null){
                return ResponseUtil.fail(-1,"商品分类不存在");
            }
        }
        return null;
    }
}
